package com.lvyou.ctrls;

import com.lvyou.model.Result;
import com.lvyou.service.LoginSrvImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
public class LoginCtrl {

    @Resource
    private LoginSrvImpl srv;

    @RequestMapping("/login")
    public ModelAndView login(String username,String password){
        ModelAndView mv=new ModelAndView();
        if(username==null || "".equals(username)){
            mv.addObject("msg","用户名不能为空！");
            mv.setViewName("loginFails");
            /*
            重定向得方式，无法通过request对象传递数据
             */
//            mv.setViewName("redirect:/loginFails.jsp?error=1");
            return mv;
        }
        if(password==null || "".equals(password)){
//            mv.addObject("msg","密码不能为空！");
//            mv.setViewName("loginFails");
            mv.setViewName("redirect:/loginFails.jsp?error=2");
            return mv;
        }

        Result res=srv.login(username,password);
        if(res.getCode()==0){
            mv.setViewName("index");
        }else{
//            mv.addObject("msg",res.getMsg());
//            mv.setViewName("loginFails");
            mv.setViewName("redirect:/loginFails.jsp?error=3");
        }
        return mv;
    }
}
